package zmj.test.javaparser.ast;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseResult;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
import com.github.javaparser.printer.YamlPrinter;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

public class AstParser {
    public static void main(String[] args) throws IOException {
        // 1. 转换的是完整的Java文件
        File base = new File("");
        String relativePath = "test-case/javaparser-testcase/src/main/java/zmj/test/thread/MyThread.java";
        String absolutePath = base.getCanonicalPath() + File.separator + relativePath;
        ParseResult<CompilationUnit> result = new JavaParser().parse(Paths.get(absolutePath));
        result.getResult().ifPresent(YamlPrinter::print);


        // 2. 转换的为部分代码片段
        ParseResult<VariableDeclarationExpr> exprResult = new JavaParser().parseVariableDeclarationExpr("int x = 3");
        System.out.println(exprResult.getResult().get());
        exprResult.getResult().ifPresent(YamlPrinter::print);
    }
}
